package com.gem.dao.impl;

import com.gem.dao.UserDAO;
import com.gem.entity.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 * @Auther: jzhang
 * @Date: 2019/12/31 16:05
 * @Description:
 */
public class UserDAOImpl implements UserDAO {
    //需要获取会话工厂对象
    private SqlSessionFactory sqlSessionFactory;

    public UserDAOImpl(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    @Override
    public User findUserById(int id) {
        //sqlsession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user = sqlSession.selectOne("test.findUserById",id);
        sqlSession.close();
        return user;
    }

    @Override
    public void insertUser(User user) {
        SqlSession sqlSession = sqlSessionFactory.openSession();

        sqlSession.insert("test.insertUser",user);

        //提交事务
        sqlSession.commit();

        sqlSession.close();
    }

    @Override
    public void deleteUser(int id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();

        sqlSession.delete("test.deleteUser",id);

        sqlSession.commit();

        sqlSession.close();
    }
}
